"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Classrooms = exports.RoomMap = exports.Room = void 0;
const Member_1 = require("./Member");
const types_1 = require("../utils/types");
const enums_1 = require("../utils/enums");
const log4js_1 = require("log4js");
const logger = (0, log4js_1.getLogger)('Classroom');
class Room extends types_1.RoomBase {
    constructor() {
        super(...arguments);
        this.members = new Member_1.RoomMemberMap();
        this.inviteMembers = [];
        this.mediaOptions = {};
        this.conferenceMode = enums_1.ConferenceModeEnums.DEFAULT;
        this.destroyer = null;
        this.speaker = null;
    }
    json(includeMembers) {
        if (includeMembers) {
            return {
                ...super.json(),
                mediaOptions: this.mediaOptions,
                members: this.getMembers(true),
            };
        }
        else {
            return {
                ...super.json(),
                mediaOptions: this.mediaOptions,
            };
        }
    }
    static create(option) {
        return new Room(option);
    }
    setInviteMembers(members) {
        this.inviteMembers = members;
    }
    getInviteMembers() {
        return this.inviteMembers;
    }
    setRecordUri(userId, uri) {
        this.inviteMembers.forEach((member) => {
            if (member.userId === userId) {
                member.uri = uri;
            }
        });
    }
    setMember(member) {
        if (!this.inviteMembers.find(({ userId }) => member.userId === userId)) {
            this.inviteMembers.push(member);
        }
        if (member instanceof Member_1.Member) {
            this.members.set(member.userId, member);
        }
        else {
            this.members.set(member.userId, new Member_1.Member(member));
        }
    }
    getMember(userId) {
        return this.members.get(userId);
    }
    hasMember(userId) {
        return this.members.has(userId);
    }
    removeMember(userId) {
        this.members.delete(userId);
    }
    async removeMemberAndSaveRecord(anyone) {
        if (anyone) {
            const uri = await anyone.stop();
            if (uri) {
                this.setRecordUri(anyone.userId, uri);
            }
            anyone.state = enums_1.MemberStateEnums.FREE;
            this.removeMember(anyone.userId);
        }
        else {
            logger.error('anyone not find!');
        }
    }
    getMembers(toBase) {
        const { members } = this;
        const __members = [];
        for (const member of members.values()) {
            if (toBase) {
                __members.push(member.json());
            }
            else {
                __members.push(member);
            }
        }
        return __members;
    }
    hasSpeaker() {
        return !!this.speaker;
    }
    setSpeaker(speaker) {
        if (!this.hasSpeaker()) {
            this.speaker = speaker;
        }
        else {
            console.error('speaker is ');
        }
    }
    getSpeaker() {
        return this.speaker;
    }
    removeSpeaker() {
        this.speaker = null;
    }
    setMediaOption(mediaOptions) {
        this.mediaOptions = mediaOptions;
    }
    getMediaOption() {
        return this.mediaOptions;
    }
    destroy() {
        var _a, _b;
        try {
            (_a = this.pipeline) === null || _a === void 0 ? void 0 : _a.release();
            this.pipeline = undefined;
            (_b = this.client) === null || _b === void 0 ? void 0 : _b.close();
            this.client = undefined;
        }
        catch (error) {
            console.error(error);
        }
    }
}
exports.Room = Room;
class RoomMap extends Map {
    toArray(toBase) {
        const data = [];
        for (const room of this.values()) {
            data.push({
                ...room.json(),
                members: room.getMembers(toBase)
            });
        }
        return data;
    }
}
exports.RoomMap = RoomMap;
exports.Classrooms = new RoomMap();
